package es.xpt.ysf.commons.report;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignParameter;
import net.sf.jasperreports.engine.util.JRProperties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import ar.com.fdvs.dj.core.CoreException;
import ar.com.fdvs.dj.core.layout.AbstractLayoutManager;
import ar.com.fdvs.dj.core.registration.ColumnRegistrationManager;
import ar.com.fdvs.dj.core.registration.DJGroupRegistrationManager;
import ar.com.fdvs.dj.domain.ColumnProperty;
import ar.com.fdvs.dj.domain.DynamicJasperDesign;
import ar.com.fdvs.dj.domain.DynamicReport;


/**
 * Helper class for running a report and some other DJ related stuff
 */
public final class DynamicJasperHelperXPT {

	private static final Log log = LogFactory.getLog(DynamicJasperHelperXPT.class);
	private static final String DJ_RESOURCE_BUNDLE ="dj-messages";
	
	private static void registerOtherFields(DynamicJasperDesign jd, List<?> fields) {
		for (Iterator<?> iter = fields.iterator(); iter.hasNext();) {
			ColumnProperty element = (ColumnProperty) iter.next();
			JRDesignField field = new JRDesignField();
			field.setValueClassName(element.getValueClassName());
			field.setName(element.getProperty());
			try {
				jd.addField(field);
			} catch (JRException e) {
				//if the field is already registered, it's not a problem
				log.warn(e.getMessage(),e);
			}
		}

	}
	
	@SuppressWarnings("unchecked")
	private final static void registerEntities(DynamicJasperDesign jd, DynamicReport dr) {
		new ColumnRegistrationManager(jd,dr).registerEntities(dr.getColumns());
		new DJGroupRegistrationManager(jd,dr).registerEntities(dr.getColumnsGroups());
		registerOtherFields(jd,dr.getFields());
		Locale locale = dr.getReportLocale() == null ? Locale.getDefault() : dr.getReportLocale();
		ResourceBundle messages = null;
		if (dr.getResourceBundle() != null ){
			try {
				messages =  ResourceBundle.getBundle(dr.getResourceBundle(), locale);
			} catch (MissingResourceException e){ log.warn(e.getMessage() + ", usign defaut (dj-messages)");}
		}

		if (messages == null) {
			messages =  ResourceBundle.getBundle(DJ_RESOURCE_BUNDLE, locale);
		}
		jd.getParametersWithValues().put(JRDesignParameter.REPORT_RESOURCE_BUNDLE, messages);
		jd.getParametersWithValues().put(JRDesignParameter.REPORT_LOCALE, locale);
	}


	//////////////*********************************************
	public final static JasperReport generateJasperReport(DynamicJasperDesign jd,DynamicReport dr, AbstractLayoutManager layoutManager) {
		log.info("generating JasperReport");
		JasperReport jr = null;
		try {
			registerEntities(jd, dr);
			layoutManager.applyLayout(jd, dr);
            JRProperties.setProperty("net.sf.jasperreports.engine.design", "ar.com.fdvs.dj.util.DJJRJdtCompiler");
            jr = JasperCompileManager.compileReport(jd);
		} catch (CoreException e) {
			log.error(e.getMessage());
		} catch (JRException e) {
			log.error(e.getMessage());
		}
		return jr;
	}
	
}
